package Array;

import java.util.Scanner;

public class Hash {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        String key="";
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("a:添加");
            System.out.println("l:显示");
            System.out.println("f:查找");
            System.out.println("d:删除");
            System.out.println("e:退出");
            key=scanner.next();
            switch (key){
                case "a":
                    System.out.println("请输入id：");
                    int id=scanner.nextInt();
                    System.out.println("请输入name：");
                    String name=scanner.next();
                    hashTab.add(new Emp(id,name));
                    break;
                case "l":
                    System.out.println("显示全部员工：");
                    hashTab.list();
                    break;
                case "f":
                    System.out.println("输入你要查找的员工id：");
                    int Id=scanner.nextInt();
                    hashTab.findEmp(Id);
                    break;
                case "d":
                    System.out.println("输入你要删除的员工id：");
                    id= scanner.nextInt();
                    hashTab.del(id);
                    break;
                case "e":
                    System.out.println("退出");
                    System.exit(0);
                    break;
            }
        }
    }
}
class Emp{
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {//构造函数
        super();
        this.id = id;
        this.name = name;
    }
}

class HashTab{
    private EmpLinkedList[] EmpLinkedListArr;//存放链表的数组
    private int size;

    public HashTab( int size) {//构造函数
        this.size = size;
        EmpLinkedListArr = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            EmpLinkedListArr[i]=new EmpLinkedList();//给每一个数组一个头
        }
    }
    public void findEmp(int id){
        int emplinkedlistNo=IdNo(id);
        Emp emps=EmpLinkedListArr[emplinkedlistNo].findEmp(id);
        System.out.println(emps.id+"   "+emps.name);
    }
    public void add(Emp emp){
        int emplinkedlistNo=IdNo(emp.id);
        EmpLinkedListArr[emplinkedlistNo].add(emp);
    }
    public void list(){
        for (int i = 0; i < size; i++) {
            EmpLinkedListArr[i].list();
        }
    }
    public void del(int id){
        int emplinkedlistNo=IdNo(id);
        EmpLinkedListArr[emplinkedlistNo].del(id);
    }
    public int IdNo(int id){
        return id % size;
    }
}
class EmpLinkedList{
    private Emp head=new Emp(0,"");
    public void add(Emp emp){
        if (head==null){
            head=emp;
            return;
        }
        boolean loop=true;
        Emp temp=head;
        while (true){
            if (temp.next==null){
                break;
            }
            if (emp.id<temp.next.id) {
                break;
            }
            if (emp.id==temp.id){
                loop=false;
                break;
            }
            temp=temp.next;
        }
        if (loop){
            emp.next=temp.next;
            temp.next=emp;
        }
    }
    public void del(int id){
        if (head==null){
            System.out.println("链表为空");
            return;
        }
        Emp temp=head;
        while (true){
            if (temp.id==id){
                break;
            }
            temp=temp.next;
        }
        temp=temp.next;
        head=temp;
        }

    public void list(){
        if (head==null){
            System.out.println("链表为空");
            return;
        }
        Emp temp=head;
        while (true){
            System.out.print("--"+temp.id+"--"+temp.name+"  ");
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        System.out.println();
    }
    public Emp findEmp(int id){
        if (head==null){
            System.out.println("链表为空");
            return null;
        }
        Emp temp=head;
        while (true){
            if (temp.id==id){
                break;
            }
            temp=temp.next;
            if (temp.next==null){
                temp=null;
            }
        }
        return temp;
    }
}